//
//  Status.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/10/14.
//  Copyright © 2015年 郭平. All rights reserved.
//

import UIKit

class Status: NSObject {

    var id: Int = 0
    var text: String?
    var user: User?                     // 转发微博
    var created_at: String?             // 创建时间
    var pic_urls: [StatusPhotoInfo]?    // 缩略图地址
    var retweeted_status: Status?       // 转发微博
    var reposts_count: Int = 0          // 转发数
    var comments_count: Int = 0         // 评论数
    var attitudes_count: Int = 0        // 点赞数
    var createdAtStr: String {          // 根据当前时间,计算出来的时间显示字符串
        
        var result = ""
        
        let formatter = NSDateFormatter()
        formatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        formatter.locale = NSLocale(localeIdentifier: "en_US")
        
        let createdDate = formatter.dateFromString(created_at!)
        
        let currentDate = NSDate()
        
        if currentDate.isThisYear(createdDate!) {
            if currentDate.isToday(createdDate!) {
                
                result = "是今天"
                
                let timeInterval = currentDate.timeIntervalSinceDate(createdDate!)
                if timeInterval < 60 {
                    result = "刚刚"
                }else if timeInterval < 60 * 60 {
                    
                    result = "\(Int(timeInterval) / 60)分钟前"
                }else {
                    result = "\(Int(timeInterval) / (60 * 60))小时前"
                }
            }else if currentDate.isYesterday(createdDate!) {  // 昨天
                
                formatter.dateFormat = "昨天 HH:mm"
                result = formatter.stringFromDate(currentDate)
            }else {  // 其他
                formatter.dateFormat = "MM-dd HH:mm"
                result = formatter.stringFromDate(currentDate)
            }
        }else {
        
            // 不是今年显示
            formatter.dateFormat = "yyyy-MM-dd"
            result = formatter.stringFromDate(createdDate!)
        }
        return result
    }
    
    var source: String?{                // 来源
    
        didSet{
         
          let sourceStr = source! as NSString
            let preRange = sourceStr.rangeOfString("\">")
            
            if preRange.location != NSNotFound {
            
                let suRange = sourceStr.rangeOfString("</")
                if suRange.location != NSNotFound {
                    
                    // 计算从哪儿开始截取
                    let location = preRange.location + preRange.length
                    
                    let length = suRange.location - location
                    source = "来自\(sourceStr.substringWithRange(NSMakeRange(location,length)))"
                }
            }
        }
    }
    
    
    init(dict: [String : AnyObject]){
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if (key as NSString).isEqualToString("user"){
            
            user = User(dict: value as! [String : AnyObject])
            
        }else if (key as NSString).isEqualToString("retweeted_status") {
            
            retweeted_status = Status(dict: (value as! [String: AnyObject]))
        }else if (key as NSString).isEqualToString("pic_urls"){
            
            pic_urls = [StatusPhotoInfo]()
            
            if let v = (value as? [[String : AnyObject]]){
                for tempV in v {
                    let photoInfo = StatusPhotoInfo(dict: tempV)
                    pic_urls?.append(photoInfo)
                }
            }
            
        }else{
            super.setValue(value, forKey: key)
        }
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
}
